import Vue from 'vue'
import Vuex from 'vuex'

// 导入axios获取数据
import { loginAPI } from '../api/login'

// 导入封装的本地存储方法
import { stroage } from '../utils/stroage'
Vue.use(Vuex)

export default new Vuex.Store({
  // 存储共享数据
  state: {
    token: stroage.get('token') || '',
    refreshToken: stroage.get('refreshToken') || '',
    userInfo: JSON.parse(stroage.get('userInfo')) || {}
  },
  // 修改数据同步唯一
  mutations: {
    settoken (state, val) {
      state.token = val
      stroage.set('token', val)
    },
    setrefreshToken (state, val) {
      state.refreshToken = val
      stroage.set('refreshToken', val)
    },
    setUserObj (state, val) {
      state.userInfo = val
      stroage.set('userInfo', val)
    }
  },
  // 异步修改数据依靠前者
  actions: {
    async  setAlltoken (store, obj) {
      const res = await loginAPI(obj)
      const { token, refresh_token: refreshToken } = res.data
      store.commit('settoken', token)
      store.commit('setrefreshToken', refreshToken)
    }

  }

})

// refresh_token: "4b6bffa4-9ccf-4719-8fd6-bc153105bfe5"
// token: "6a2853a8-
